'use strict';

define(['controllers', '../service/schedulerService'], function (controllers) {
    controllers.system.register('SchedulerController', ['$scope', 'SchedulerService', function ($scope, schedulerService) {
        $scope.init = function () {
            $scope.pageNo = 1;
            $scope.pageSize = 10;

            $scope.STATAUS_ENABLED = 1;
            $scope.STATAUS_DISABLED = 0;
        };

        $scope.initEvent = function() {
            $('#editDialog').on('shown.bs.modal', function () {
                if ($scope.save_id != null) {
                    $('#save_expression').select();
                } else {
                    $('#save_name').select();
                }
            });
        }

        $scope.initEditForm = function (scheduler) {
            scheduler = scheduler == null ? {} : scheduler;
            $scope.save_id = scheduler.id;
            $scope.save_name = scheduler.name;
            $scope.save_expression = scheduler.expression;
            $scope.save_status = scheduler.status;
            $scope.save_bean = scheduler.bean;
            $scope.save_method = scheduler.method;
            $scope.save_remark= scheduler.remark;
        };

        $scope.query = function (pageNo) {
            if (pageNo != null) {
                $scope.pageNo = pageNo;
            }
            $scope.paged = schedulerService.query({
                name: $scope.query_name,
                remark: $scope.query_remark,
                status: $scope.query_status,
                pageNo: $scope.pageNo,
                pageSize: $scope.pageSize
            });
        };
        $scope.edit = function (scheduler) {
            $scope.initEditForm(scheduler);
            if (scheduler != null) {
                $scope.editDialogTitle = '编辑调度';
                $('#save_name').prop('disabled', true);
                $('#save_bean').prop('disabled', true);
                $('#save_method').prop('disabled', true);
            } else {
                $scope.editDialogTitle = '创建调度';
                $('#save_name').prop('disabled', false);
                $('#save_bean').prop('disabled', false);
                $('#save_method').prop('disabled', false);
            }
            $('#editDialog').modal('show');
        };
        $scope.save = function () {
            schedulerService.save({
                id: $scope.save_id
            }, {
                name: $scope.save_name,
                bean: $scope.save_bean,
                method: $scope.save_method,
                expression: $scope.save_expression,
                remark: $scope.save_remark
            }, function () {
                $('#editDialog').modal('hide');
                $scope.query();
                $scope.setEditForm(null);
            });
        };

        $scope.remove = function (id) {
            schedulerService.remove({id: id});
        };

        $scope.syncing = false;
        $scope.sync = function () {
            $scope.syncing = true;
            schedulerService.sync(function () {
                setTimeout(function() {
                    $scope.$apply(function() {
                        $scope.syncing = false;
                    });
                }, 1000);
            });
        };

        $scope.stop = function (id) {
            schedulerService.stop({id: id}, function () {
                $scope.query();
            });
        };

        $scope.start = function (id) {
            schedulerService.start({id: id}, function () {
                $scope.query();
            });
        };

        $scope.running = {};
        $scope.run = function (id) {
            $scope.running[id] = true;
            schedulerService.run({id: id}, function () {
                setTimeout(function() {
                    $scope.$apply(function() {
                        $scope.query();
                        $scope.running[id] = false;
                    });
                }, 1000);
            });
        };

        $scope.confirmRemove = function (id) {
            $scope.removeId = id;
            $('#removeDialog').modal('show');
        };

        $scope.remove = function () {
            schedulerService.remove({id: $scope.removeId}, function () {
                $scope.removeId = null;
                $('#removeDialog').modal('hide');
                $scope.query();
            });
        };

        $scope.init();
        $scope.initEvent();
        $scope.query();
    }]);
});